package com.yingran.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yingran.common.util.AndroidUtil;


/**
 * Created by Ksy
 */

public class HeadBar extends RelativeLayout {

    private Context context;
    private TextView tv_title;
    private LinearLayout ll_leftContainer;
    private LinearLayout ll_centerContainer;
    private LinearLayout ll_rightContainer;
    private ImageView iv_right;
    private TextView tv_right;
    private ImageView iv_left;
    private DisplayMetrics displayMetrics;
    private int colorResId = R.color.common_colorWhite;
    private int dimension;

    public HeadBar(Context context) {
        this(context, null);
    }

    public HeadBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.common_layout_headbar, this, true);
        dimension = getResources().getDimensionPixelSize(R.dimen.common_headBarHeight);

        ll_leftContainer = findViewById(R.id.ll_leftContainer);
        ll_centerContainer = findViewById(R.id.ll_centerContainer);
        ll_rightContainer = findViewById(R.id.ll_rightContainer);


        LayoutParams paramsL = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension);
        paramsL.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        ll_leftContainer.setLayoutParams(paramsL);
        ll_leftContainer.setGravity(Gravity.CENTER);

        LayoutParams paramsR = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension);
        paramsR.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        paramsR.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        ll_rightContainer.setLayoutParams(paramsR);
        ll_rightContainer.setGravity(Gravity.CENTER);

        LayoutParams paramsC = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension);
        paramsC.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        paramsC.height = dimension;
//        ll_centerContainer.l(dp2Px(100), dp2Px(0), dp2Px(0), dp2Px(100));
        ll_centerContainer.setGravity(Gravity.CENTER);
        ll_centerContainer.setLayoutParams(paramsC);

        int statusBarHeight = AndroidUtil.getStatusBarHeight(context);
        setPadding(0, statusBarHeight, 0, 0);
    }

    public HeadBar setTitle(String title) {
        ll_leftContainer.setVisibility(VISIBLE);
        ll_rightContainer.setVisibility(VISIBLE);
        ll_centerContainer.removeAllViews();
        if (tv_title == null) {
            tv_title = generateTitleTextView(true);
        }
        tv_title.setText(title);
        ll_centerContainer.addView(tv_title);
        return this;
    }

    public HeadBar setTitleColor(int colorID) {
        tv_title.setTextColor(getResources().getColor(colorID));
        return this;
    }


    public HeadBar setRightText(String text, OnClickListener clickListener) {
        ll_rightContainer.removeAllViews();
        if (tv_right == null) {
            tv_right = generateTitleTextView(false);
        }
        tv_right.setText(text);
        tv_right.setOnClickListener(clickListener);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        params.rightMargin = dp2Px(10);
        params.gravity = Gravity.CENTER;
        ll_rightContainer.addView(tv_right, params);
        ll_rightContainer.setVisibility(VISIBLE);
        return this;
    }

    private TextView generateTitleTextView(boolean isTitle) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setGravity(Gravity.CENTER);
        if (isTitle) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        } else {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            textView.setPadding(dp2Px(12), 0, dp2Px(12), 0);
        }
        params.leftMargin = AndroidUtil.dip2px(80);
        params.rightMargin = AndroidUtil.dip2px(80);
        textView.setTextColor(getResources().getColor(colorResId));
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setLayoutParams(params);
        return textView;
    }

    private int dp2Px(int dp) {
        if (displayMetrics == null) {
            displayMetrics = context.getResources().getDisplayMetrics();
        }
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
    }

    public HeadBar setRightIcon(int resID, OnClickListener clickListener) {
        ll_rightContainer.removeAllViews();
        if (iv_right == null) {
            iv_right = generateLeftOrRightImageView();
        }
        iv_right.setImageResource(resID);
        iv_right.setOnClickListener(clickListener);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp2Px(35), dp2Px(35));
        params.rightMargin = dp2Px(5);
        params.gravity = Gravity.CENTER;
        ll_rightContainer.addView(iv_right, params);
        ll_rightContainer.setVisibility(VISIBLE);
        return this;
    }

    public HeadBar setRightIcon(int resID) {
        ll_rightContainer.removeAllViews();
        if (iv_right == null) {
            iv_right = generateLeftOrRightImageView();
        }
        iv_right.setImageResource(resID);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp2Px(35), dp2Px(35));
        params.rightMargin = dp2Px(5);
        params.gravity = Gravity.CENTER;
        ll_rightContainer.addView(iv_right, params);
        ll_rightContainer.setVisibility(VISIBLE);
        return this;
    }


    public HeadBar setLeftIcon(int resID, OnClickListener clickListener) {
        ll_leftContainer.removeAllViews();
        if (iv_left == null) {
            iv_left = generateLeftOrRightImageView();
        }
        iv_left.setImageResource(resID);
        iv_left.setOnClickListener(clickListener);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp2Px(35), dp2Px(35));
        params.leftMargin = dp2Px(5);
        params.gravity = Gravity.CENTER;
        ll_leftContainer.addView(iv_left, params);
        ll_leftContainer.setVisibility(VISIBLE);
        return this;
    }


    public HeadBar setLeftIcon(int resID) {
        ll_leftContainer.removeAllViews();
        if (iv_left == null) {
            iv_left = generateLeftOrRightImageView();
        }
        iv_left.setImageResource(resID);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dp2Px(35), dp2Px(35));
        params.leftMargin = dp2Px(5);
        params.gravity = Gravity.CENTER;
        ll_leftContainer.addView(iv_left, params);
        ll_leftContainer.setVisibility(VISIBLE);
        return this;
    }


    public HeadBar setLeftIconVisible(boolean isShowLeftButton) {
        if (ll_leftContainer != null) {
            ll_leftContainer.setVisibility(isShowLeftButton ? VISIBLE : GONE);
        }
        return this;
    }

    public HeadBar setLeftIconClickListener(OnClickListener clickListener) {
        if (ll_leftContainer != null) {
            ll_leftContainer.setOnClickListener(clickListener);
        }
        return this;
    }

    public HeadBar setTitleClickListener(OnClickListener clickListener) {
        if (tv_title != null) {
            tv_title.setOnClickListener(clickListener);
        }
        return this;
    }


    private ImageView generateLeftOrRightImageView() {
        ImageView icon = new ImageView(getContext());
        icon.setScaleType(ImageView.ScaleType.CENTER);
        return icon;
    }

    @Override
    public void setBackgroundColor(int color) {
        super.setBackgroundColor(color);
    }

    public LinearLayout getLeftContainer() {
        return ll_leftContainer;
    }


    public View getRightIcon() {
        return iv_right;
    }
}
